package com.example.demo.database;

import java.sql.Array;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DataCon {
	 //首先连接到数据库的URL
    private static String url = "jdbc:mysql://120.77.202.109:3306/dfyz_crm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8";/*jdbc协议:数据库子协议:主机:端口/连接的数据库*/
    private static String user = "dfyz";//数据库用户名
    private static String password = "Dfyz2019.qaz";//数据库密码
    
   public static void main(String[] args) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
	
    	//1.创建驱动程序类对象
//        Driver driver = new com.mysql.cj.jdbc.Drive();//需要导入上面提到的jar包
        Driver driver = (Driver)Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        //设置用户名和密码
        Properties pro = new Properties();
        pro.setProperty("user",user);
        pro.setProperty("password",password);
        //2.连接数据库
        Connection conn = driver.connect(url,pro);
        //测试是否连接成功
        boolean valid = conn.isValid(0);
        System.err.println(valid);
        System.out.println(conn);
        String sql = "select * from hrm_human ";
        Statement createStatement = conn.createStatement();
        ResultSet resultSet = createStatement.executeQuery(sql);
        while (resultSet.next()) {
        	System.err.println(resultSet.getString(1));
        	System.err.println(resultSet.getString(2));
		}
        
        System.err.println("=========");
        
      //创建驱动程序类对象
//        Driver dirver = new com.mysql.cj.jdbc.Driver();   

        /*Mysql*/
        //Driver driver2 = new com.oracle.jdbc.Driver();/*oracle*/
        //1.注册驱动程序(可以注册多个)
        DriverManager.registerDriver(driver);
        //2.建立连接到数据库
        Connection conn2 = DriverManager.getConnection(url, user, password);
        //测试是否连接成功
        System.out.println(conn);
        PreparedStatement prepareStatement2 = conn2.prepareStatement(sql);
        ResultSet resultSet2 = prepareStatement2.executeQuery();
        while (resultSet2.next()) {
        	System.err.println(resultSet2.getString(4));
        	System.err.println(resultSet2.getString(3));
    
			
		}    }
}
